package com.mef.smsplat.service;

import java.util.List;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mef.smsplat.dao.AirportInfoDao;
import com.mef.smsplat.dao.base.EntityDao;
import com.mef.smsplat.domain.AirportInfoDomain;
import com.mef.smsplat.domain.LimitInfoDomain;
import com.mef.smsplat.service.base.BaseService;

@Service
@Transactional
public class AirportInfoService extends BaseService<LimitInfoDomain, String> {

	private AirportInfoDao airportInfoDao;
	private CacheManager cacheManager;

	@Override
	public EntityDao getEntityDao() {
		return this.airportInfoDao;
	}

	public void setAirportInfoDao(AirportInfoDao airportInfoDao) {
		this.airportInfoDao = airportInfoDao;
	}

	public void setCacheManager(CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}

	/**
	 * 初始化机场缓存
	 */
	@Transactional(readOnly=true)
	public void initCache() {
		List<AirportInfoDomain> list = airportInfoDao.findAll();
		Cache cache=cacheManager.getCache("staticCache");//永不销毁的Cache
		for(int i=0;i<list.size();i++){
			AirportInfoDomain domain=list.get(i);
			Element e=new Element(domain.getCode(), domain);//key-val==code-domain
			cache.put(e);//放入缓存
		}
	}
	
	/**
	 * 根据三字码得到机场信息，不存在则返回null
	 * @param code
	 * @return
	 */
	@Transactional(readOnly=true)
	public AirportInfoDomain getAirportByCode(String code){
		Cache cache=cacheManager.getCache("staticCahce");
		Element e = cache.get(code);
		if(e==null)
			return null;
		return (AirportInfoDomain) e.getValue();
	}

}
